package com.tgpms.web.cost.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.util.VformBeanUtil;
import com.tgpms.web.cost.dao.DetailEstimateItemsMapper;
import com.tgpms.web.cost.entity.DetailEstimateItems;
import com.tgpms.web.cost.entity.DetailEstimateItemsKey;
import com.tgpms.web.cost.service.DetailEstimateItemsService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class DetailEstimateItemsServiceImpl implements DetailEstimateItemsService {

	@Autowired
	private VformInterface vformInterface;
	
	
	/**
	 * @Author newway
	 * @Description 查询概算细项信息
	 * @Date 2019年8月22日下午5:41:56
	 * @Param pcsCode
	 * @return List<DetailEstimateItems>
	 **/
	@Override
	public Result selDetailEstimateList(PageInfo pageInfo) {
		JSONObject param = new JSONObject();
		Object selparam = pageInfo.getQueryMap().get("selparam");
		Object leftdition = pageInfo.getQueryMap().get("leftdition");
		Object condition = pageInfo.getQueryMap().get("condition");
		Object dataowner = pageInfo.getQueryMap().get("dataowner");
		if(Common.isEmpty(selparam)) {
			param.put("[SQL1]", " ");
		}else {
			param.put("[SQL1]", selparam.toString());
		}
		if(Common.isEmpty(leftdition)) {
			param.put("[SQL2]", " ");
		}else {
			param.put("[SQL2]", leftdition.toString());
		}
		if(Common.isEmpty(condition)) {
			param.put("[SQL3]", " ");
		}else {
			param.put("[SQL3]", condition.toString());
		}
		Result result = vformInterface.multiForMsQueryBySql("SCWZC019", param.toJSONString(), pageInfo.getPage(), 
				pageInfo.getSize(), "Y", "Y", dataowner.toString());
		return result;
	}

	/**
	* @Author newway
	* @Description 添加工程概算汇总信息
	* @Date 2019年8月6日下午5:57:59
	* @Param com.tgpms.web.cost.entity.DetailEstimateItems
	* @return Integer
	**/
	@Override
	public Map<String,String> addDetailEstimateInfo(DetailEstimateItems detailEstimateItems) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject checkno = new JSONObject();
		checkno.put("PCS_CODE=", detailEstimateItems.getPcsCode());
		//查询vform主键
		Result findReslut = vformInterface.find("D3010", checkno.toJSONString(), "1", "100", "N", "N", "");
		System.out.println("vform查询返回结果为:  "+findReslut);
		if(findReslut==null || !findReslut.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		if(findReslut.getData().toString().equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		if(findReslut.getData().toString().equals("[]")) {
			detailEstimateItems.setEstimateItem("1");
			detailEstimateItems.setSubItem("1");
		}else {
			List<Map<String,String>> list = JSONArray.parseObject(findReslut.getData().toString(), List.class);
			List<Integer> fixnos = new ArrayList<Integer>();
			for(Map<String,String> m : list) {
				fixnos.add(Integer.parseInt(m.get("SUB_ITEM").toString()));
			}
			Integer max = Collections.max(fixnos);
			detailEstimateItems.setEstimateItem((max+1)+"");
			detailEstimateItems.setSubItem((max+1)+"");
		}
		//对象转json字符串, 字段驼峰转下划线大写字段
		JSONObject json = VformBeanUtil.entityToJsonString(detailEstimateItems);
		if(json==null || json.isEmpty()) {
			map.put("code", "-2");
			map.put("info", ExceptionExplain.ERROR_BY_PARSING.getExplain());
			return map;
		}
		System.out.println("vform增加上送参数   "+json.toString());
		//vform同步插入
		Result result = vformInterface.insert("D3010", json);
		System.out.println("vform增加返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
		}
		
		return map;
	}

	/**
	* @Author newway
	* @Description 更新工程概算汇总信息
	* @Date 2019年8月8日上午9:18:35
	* @Param com.tgpms.web.cost.entity.DetailEstimateItems
	* @return Integer
	**/
	@Override
	public Map<String,String> updateDetailEstimateInfo(DetailEstimateItems detailEstimateItems) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("PCS_CODE", detailEstimateItems.getPcsCode());
		keyjson.put("ESTIMATE_ITEM", detailEstimateItems.getEstimateItem());
		keyjson.put("SUB_ITEM", detailEstimateItems.getSubItem());
		JSONObject pajson = new JSONObject();
		pajson = VformBeanUtil.entityToJsonStringByUpdate(detailEstimateItems);
		pajson.remove("PCS_CODE");
		pajson.remove("ESTIMATE_ITEM");
		pajson.remove("SUB_ITEM");

		System.out.println("vform更新上送主键   "+keyjson.toString());
		System.out.println("vform更新上送参数   "+pajson.toString());
		//vform同步更新
		Result result = vformInterface.update("D3010", keyjson.toString(),pajson.toString(),"Y","Y",detailEstimateItems.getDataOwner());
		System.out.println("vform更新返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}
		return map;
	}

	/**
	* @Author newway
	* @Description 删除工程概算细项信息
	* @Date 2019年8月8日上午9:48:24
	* @Param com.tgpms.web.cost.entity.DetailEstimateItemsKey
	* @return Integer
	**/
	@Override
	public Map<String,String> deleteDetailEstimateInfo(DetailEstimateItemsKey detailEstimateItemsKey) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("PCS_CODE", detailEstimateItemsKey.getPcsCode());
		keyjson.put("ESTIMATE_ITEM", detailEstimateItemsKey.getEstimateItem());
		keyjson.put("SUB_ITEM", detailEstimateItemsKey.getSubItem());
		System.out.println("vform删除上送主键   "+keyjson.toString());
		//vform同步删除
		Result result = vformInterface.delete("D3010", keyjson.toJSONString());
		System.out.println("vform删除返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}
		return map;
	}

	

}
